<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `5 10 20 50 85 1`;
        let list = input.split(' ').map(v => parseInt(v));
        let stack = [];
        console.log(list);  

        let i = 0


        while (list.length>0) {
            let t = list.shift()
            stack.push(t)
            if (stack.length > 1) {
                let sum = 0;
                let count = 0;
                let tag = stack[stack.length - 1]

                for (let j = stack.length - 2; j >= 0; j--) {
                    sum += stack[j]
                    count++                    
                    if (sum === tag) {                                 
                        stack.splice(j, count+1)
                        count = 0
                        sum=0
                        list.unshift(t*2)
                    }
                }
            }
        }

        console.log(stack.reverse());

    </script>
</body>

</html>